using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace ECommerce.Library.Web
{
    public class CacheManager<TGenericType>
    {
        public static TGenericType GetValue(string key)
        {
            return (TGenericType)HttpRuntime.Cache[key];    
        }

        public static void SetValue(string key, TGenericType value)
        {
            if (HttpRuntime.Cache[key] != null)
                throw new InvalidOperationException("The cache key is already in use by " +
                    GetValue(key).GetType().ToString());
            else
            {
                Cache DataCache = HttpRuntime.Cache;

                //we are putting the cache without any expiration policy, 
                //we are expiring the cache programatically once when a new or existing instance of the current entity are being updated.
                DataCache.Insert(key, value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
            }
        }

        public static void Remove(string key)
        {
            HttpRuntime.Cache.Remove(key);
        }
    }
}
